<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style lang="scss">
    /* 方式1 flex布局*/
    
    .container1 {
        background-color: pink;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    .container1>div {
        width: 200px;
        height: 200px;
        background-color: skyblue;
    }
    /* 方式2:只使用margin:auto 条件是知道父盒子的固定长宽 但是只能水平居中*/
    
    .container2 {
        background-color: pink;
        width: 1400px;
        height: 400px;
        margin-top: 20px;
    }
    
    .container2>div {
        width: 200px;
        height: 200px;
        margin: auto;
        background-color: skyblue;
    }
    /* 方式3 使用定位position top和left都为一半,但是这一半是根据body来说的,所以要根据transform:translate和margin-top负值来减去自己盒子的一半 */
    
    .container3 {
        background-color: pink;
        margin-top: 20px;
        position: relative;
    }
    
    .container3>div {
        width: 200px;
        height: 200px;
        background-color: skyblue;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-top: -100px;
        margin-left: -100px;
    }
    
    .container4 {
        background-color: pink;
        margin-top: 20px;
        position: relative;
    }
    
    .container4>div {
        width: 200px;
        height: 200px;
        background-color: skyblue;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        /* transform里面的百分数是根据自身宽高来定的 */
    }
</style>

<body>
    <div class="container1">
        <div>1</div>
    </div>
    <div class="container2">
        <div>2</div>
    </div>
    <div class="container3">
        <div>3</div>
    </div>
    <div class="container4">
        <div>4</div>
    </div>
</body>

</html>